
package com.jf.cloud.product.controller.platform;

import com.jf.cloud.api.product.dto.CategoryShopDTO;
import com.jf.cloud.api.product.dto.HandleCategoryShopDTO;
import com.jf.cloud.common.i18n.I18nMessage;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.ExcelUtil;
import com.jf.cloud.product.service.CategoryService;
import com.jf.cloud.product.service.CategoryShopService;
import com.jf.cloud.product.vo.CategoryPlatformExcelVO;
import com.jf.cloud.product.vo.CategoryShopVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author zz
 * @Date 2021/4/26 9:22
 */
@RestController("platformCategoryController")
@RequestMapping("/p/category")
@Tag(name = "platform-分类信息")
public class CategoryController {

    @Autowired
    private CategoryShopService categoryShopService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/signing_info_by_shopId")
    @Operation(summary = "根据店铺id获取分类签约信息" , description = "根据店铺id获取分类签约信息")
    @Parameter(name = "sysType", description = "系统类型 0.普通用户系统 1.商家端 2.平台端 3.供应商端" )
    public ServerResponseEntity<List<CategoryShopVO>> listByShopId(@RequestParam(value = "shopId") Long shopId,@RequestParam(value = "sysType", required = false) Integer sysType) {
        sysType = Objects.isNull(sysType) ? AuthUserContext.get().getSysType() : sysType;
        List<CategoryShopVO> categoryShopVOList = categoryShopService.listByShopId(shopId, sysType,I18nMessage.getLang());
        return ServerResponseEntity.success(categoryShopVOList);
    }


    @PutMapping("/signing")
    @Operation(summary = "更新店铺签约分类" , description = "更新店铺签约分类")
    public ServerResponseEntity<Void> signing(@Valid @RequestBody HandleCategoryShopDTO handleCategoryShopDTO) {
        List<CategoryShopDTO> categoryShopDTOList = handleCategoryShopDTO.getCategoryShopDTOList();
        Integer sysType = handleCategoryShopDTO.getSysType();
        Long shopId = handleCategoryShopDTO.getShopId();
        sysType = Objects.isNull(sysType) ? AuthUserContext.get().getSysType() : sysType;
        categoryShopService.signingCategory(categoryShopDTOList, shopId,sysType);
        return ServerResponseEntity.success();
    }

    @GetMapping("/sold_excel")
    @Operation(summary = "导出excel" , description = "导出分类excel")
    public ServerResponseEntity<Void> spuSoldExcel(HttpServletResponse response) {
        List<CategoryPlatformExcelVO> list = categoryService.listExcelPlatform();
        ExcelUtil.soleExcel(response, list, CategoryPlatformExcelVO.EXCEL_NAME, CategoryPlatformExcelVO.MERGE_ROW_INDEX, CategoryPlatformExcelVO.MERGE_COLUMN_INDEX,  CategoryPlatformExcelVO.class);
        return ServerResponseEntity.success();
    }
}
